package com.yuyou.fn.platform.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.platform.constant.AdminResourceEnum;
import com.yuyou.fn.platform.entity.SysRoleResource;
import com.yuyou.fn.platform.entity.SysUserRole;
import com.yuyou.fn.platform.mapper.SysRoleResourceMapper;
import com.yuyou.fn.platform.service.ISysRoleResourceService;
import com.yuyou.fn.platform.service.ISysUserRoleService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * SysRoleResource 表数据服务层接口实现类
 *
 */
@Service
public class SysRoleResourceServiceImpl extends SuperServiceImpl<SysRoleResourceMapper, SysRoleResource,Long> implements ISysRoleResourceService {

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @Override
    public List<SysRoleResource> findRoleResourceByRoleId(Long roleId) {

        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andEqualTo("roleId",roleId) ;
        return findListByExample(example);
    }

    @Override
    public List<SysRoleResource> findRoleResourceByRoleIds(List<Long> roleIds) {
        if (Utils.checkListEmpty(roleIds)){
            return Collections.EMPTY_LIST;
        }
        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andIn("roleId",roleIds);
        return findListByExample(example);
    }

    @CacheEvict(value = "CacheOfResource",allEntries = true )
    @Transactional
    @Override
    public void saveOrUpdateRoleResource(List<Long> resourceIds, Long roleId) {
        if(roleId == null ){
            throw new NullPointerException();
        }

        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andEqualTo("roleId" , roleId );

        deleteByExample(example);

        List<SysRoleResource> sysRoleResourceList = new ArrayList<>(resourceIds.size());

        for(Long resourceId : resourceIds ){
            SysRoleResource sysRoleResource = new SysRoleResource();
            sysRoleResource.setRoleId(roleId);
            sysRoleResource.setResourceId(resourceId);
            sysRoleResourceList.add(sysRoleResource);
        }
        checkIfAdmin(roleId,resourceIds);

        insertBatch(sysRoleResourceList);
    }

    private void checkIfAdmin(Long roleId,List<Long> resourceIds){
        if (AdminResourceEnum.adminRole.id.equals(roleId)){
            if (resourceIds==null || !resourceIds.contains(AdminResourceEnum.system.id) || !resourceIds.contains(AdminResourceEnum.userResource.id) || !resourceIds.contains(AdminResourceEnum.roleResource.id)){
                throw new BusException("系统管理员必须拥有系统管理(用户管理，操作权限管理)的权限");
            }
        }
    }

    @CacheEvict(value = "CacheOfResource",allEntries = true )
    @Override
    public void deleteRoleResource(Long roleId) {
        if(roleId == null ){
            throw new NullPointerException();
        }
        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andEqualTo("roleId" , roleId );
        deleteByExample(example);
    }

    @Override
    public long checkUserHaveResource(long userId, long resourceId) {
        List<SysUserRole> sysUserRoleList = sysUserRoleService.findRoleByUserId(userId);
        List<Long> roleIds = BeanUtils.toList(sysUserRoleList , "roleId");
        if (Utils.checkListEmpty(roleIds)){
            return 0;
        }
        Example<SysRoleResource> example = SysRoleResource.createExample();
        example.createCriteria().andIn("roleId",roleIds).andEqualTo("resourceId",resourceId);
        return countByExample(example);
    }
}